clear all;
close all;
clc
% Subjects = {'A63','A64','A68','A69','A70','A72','A73','A74','A75','A76','A77','A78','A79','A80','A81','A82'};
Subjects = {'A63','A64'};
Channels = 1:31;
fs = 500;
dt = 1/fs;
All_Mu=[11 10 11 11 11 10 10 11 13 8 11 11 12 10 11 10]
f_Conditions_EMG_EEG_up
finger_names = {'Thumb';'Index';'Middle';'Ring';'Pinky'};
fingers = 'abcde';
pcolor = 'kbr';
Pre = 0; % on
dur = 3.5; % off
tsig_ori=Pre+dt:dt:dur;
NN = length(tsig_ori);
plot_triggers = 0;

for iband = 1:4
    for ifinger = 1:5
        power_allsubs{iband,ifinger} = zeros(1,31);
    end
end
for isub = 1:length(Subjects)
    Sub = Subjects{isub};
    Mu = All_Mu(isub);
    f_bands = [
        0.1 2.5 % SCP
        10 10 % Mu
        13 30 % betta
        30 50];% gamma
    for iband=1:size(f_bands,1)
        if iband==2 % Mu
            b(iband,:)=fir1(100,[Mu-1 Mu+1]./(fs/2));
        else
            b(iband,:)=fir1(100,f_bands(iband,:)./(fs/2));
        end

        % for ifinger = 1:5
        %     ERP{iband,ifinger}= [];
        % end
    end
    eval(['condall=' Sub '_all;']);

    % for isession = 1:N_column:length(condall)
    for isession = 1:N_column:20

        chunkline = {condall{isession:isession+N_column-1}};
        if mod(isession,80)==1
            chunkline
        end
        finger = chunkline{1}(end);
        ifinger = find(fingers==finger);
        fname = ['EEG_data\' chunkline{2} ];
        eval(['load ' fname(1:end-4) '_P']);
        new_data2=f_montage(new_data);


        for iband=1:size(f_bands,1)

            for jc_channel = Channels
                xdata = conv(new_data2(jc_channel,:),b(iband,:),'same');
                new_data3(jc_channel,:)=xdata;
                p_all(jc_channel) = mean(new_data2(jc_channel,:).^2);
            end
            for i = 1:length(newTR_time) %  all triggers
                Ttime = newTR_time(i);
                [YY Ind] = min(abs(Ttime-tsig_long));
                for jc_channel = Channels
                    chunk = new_data3(jc_channel,Ind+round((1+Pre)*fs):Ind+round((1+Pre)*fs)+NN-1);
                    %                     power(jc_channel) = mean(chunk.^2);
                    power(jc_channel) = mean(chunk.^2)./ p_all(jc_channel);
                end
                % ERP{ifinger}=[ ERP{ifinger}
                % power];
                power_allsubs{iband,ifinger}=power_allsubs{iband,ifinger}+power;
            end
            clear new_data3;
            pause(0.1)
        end
    end % isession


    clear ERP
    pause(0.1)
end % isub
maxP = zeros(1,4);
for iband = 1:4
    for ifinger = 1:5        
        input = power_allsubs{iband,ifinger}./20;
        if max(input)>maxP(iband)
            maxP(iband) = max(input);
        end
    end
end


f1=figure
for iband = 1:4
    for ifinger = 1:5
        subplot(4,5,(iband-1)*5+ifinger)
        input = power_allsubs{iband,ifinger}./20;
        f_plot_heatmap(31,input,0,maxP(iband).*0.8,0)
        hold on;
    end
end

exportgraphics(f1,['Figure8_heatmap.png'],'Resolution',300);


f2=figure
for iband = 1:4
    for ifinger = 1:1
        subplot(4,1,(iband-1)*1+ifinger)
        input = power_allsubs{iband,ifinger}./20;
        f_plot_heatmap(31,input,0,maxP(iband).*0.8,0)
        colorbar
        hold on;
    end
end

exportgraphics(f2,['Figure_colorbar.png'],'Resolution',300);

